package com.zzw.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.reggie.common.R;
import com.zzw.reggie.dto.DishDto;
import com.zzw.reggie.dto.SetmealDto;
import com.zzw.reggie.entity.*;
import com.zzw.reggie.service.ICategoryService;
import com.zzw.reggie.service.ISetmealDishService;
import com.zzw.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        log.info(setmealDto.toString());
        setmealService.saveSetmealWithDish(setmealDto);
        return R.success("新增成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage<SetmealDto>> page(Integer page,Integer pageSize,String name){

        //构建分页构造器
        IPage<Setmeal> pageInfo = new Page<>(page,pageSize);
        IPage<SetmealDto> dtoIPage = new Page<>();
        //构建条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Setmeal::getName,name);
        //添加排序条件 根据更新时间进行降序排序
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //调用分页查询方法
        setmealService.page(pageInfo,lqw);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoIPage,"records");

        //设置集合数据
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();

        //处理集合数据
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(record,setmealDto);
            //通过id查找名字
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //给setmealDot对象设置name
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);
        }
        dtoIPage.setRecords(setmealDtos);
        return R.success(dtoIPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        log.info(ids.toString());
        setmealService.removeSetmealWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @GetMapping("/list")
    public R<List<SetmealDto>> list(Setmeal setmeal){

        //创建条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加等值条件
        lqw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        //查询状态码为1的菜品
        lqw.eq(Setmeal::getStatus,1);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //查询
        List<Setmeal> list = setmealService.list(lqw);

        List<SetmealDto> setmealDtos = new ArrayList<>();

        //循环拿到菜品分类的id 再通过查询数据库拿到分类的名称 在复制
        for (Setmeal record : list) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);

            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getDishId,record.getId());
            List<SetmealDish> setmealDishes = setmealDishService.list(lambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishes);
            setmealDtos.add(setmealDto);
        }

        return R.success(setmealDtos);
    }
}
